﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MineFlagger
{
    public partial class MainForm : Form
    {
        public static readonly string AppName = "MineFlagger";
        public static readonly string AppVersion = "v0.0.2";

        GameBoard gameBoard;
        public Random rnd;
        IViewer view;

        public MainForm()
        {
            InitializeComponent();

            this.Text = AppName + " " + AppVersion;

            rnd = new Random(~unchecked((int)DateTime.Now.Ticks));
            view = gameBoardView1;

            SetGameBoard(new GameBoard(5, 5, 4, rnd));

            smileButton.Click += new EventHandler(smileButton_Click);
            gameBoardView1.SizeChanged += new EventHandler(gameBoardView1_SizeChanged);
            gameBoardView1.PropertyChanged += new PropertyChangedEventHandler(gameBoardView1_PropertyChanged);
            
            Load += new EventHandler(MainForm_Load);
        }

        void gameBoardView1_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsDepressed")
            {
                validateSmileButton();
            }
        }

        public void SetGameBoard(GameBoard gameBoard)
        {
            GameBoard oldGameBoard = this.gameBoard;

            this.gameBoard = gameBoard;

            gameBoardView1.GameBoard = gameBoard;
            gameBoard.PropertyChanged += new PropertyChangedEventHandler(gameBoard_PropertyChanged);

            if (aiToolWindow != null)
                aiToolWindow.SetGameBoard(gameBoard);

            if (oldGameBoard != null)
                oldGameBoard.Dispose();
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            newGame(30, 16, 99);
            aIToolsToolStripMenuItem_Click(this, null);
        }


        

        void gameBoard_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (InvokeRequired)
                Invoke(new PropertyChangedEventHandler(gameBoard_PropertyChanged), sender, e);


            if (e.PropertyName == "PlayState")
            {
                validateSmileButton();

            }
            else if (e.PropertyName == "MinesRemaining")
            {
                minesLeftDigits.Value = gameBoard.MinesRemaining;
            }
            else if (e.PropertyName == "Time")
            {
                timeDigits.Value = gameBoard.Time;
            }
        }

        void validateSmileButton()
        {
            if (gameBoardView1.IsDepressed)
            {
                smileButton.SmileType = SmileType.Worried;
            }
            else
            {

                if (gameBoard.PlayState == PlayState.Lost)
                    smileButton.SmileType = SmileType.Sad;
                else if (gameBoard.PlayState == PlayState.Won)
                    smileButton.SmileType = SmileType.Cool;
                else
                    smileButton.SmileType = SmileType.Happy;
            }
        }

        void newGame(int x, int y, int mines)
        {
            gameBoard.NewGame(x, y, mines);
            gameBoardView1.Invalidate();
        }
        void newGame()
        {
            newGame(gameBoard.Width, gameBoard.Height, gameBoard.MineCount);
        }


        // new game button
        private void smileButton_Click(object sender, EventArgs e)
        {
            newGame();
        }



        protected override void OnClosing(CancelEventArgs e)
        {
            if (aiToolWindow != null)
            {
                aiToolWindow.Close();
                Application.DoEvents();
            }

            if (gameBoard != null)
            {
                gameBoard.Dispose();
                Application.DoEvents(); // finish any work that was generated by GameBoard (i.e.: Time incriments) before we dispose
            }

            base.OnClosing(e);
        }




        #region Form Resizing

        int topMargin = 60;
        int leftMargin = 5;
        int rightMargin = 5;
        int bottomMargin = 5;

        void gameBoardView1_SizeChanged(object sender, EventArgs e)
        {
            gameBoardView1.Location = new Point(leftMargin, topMargin);

            ClientSize = new Size(leftMargin + gameBoardView1.Width + rightMargin,
            topMargin + gameBoardView1.Height + bottomMargin);

            smileButton.Left = (ClientSize.Width - smileButton.Width) / 2;
        }

        #endregion


        AIToolWindow aiToolWindow;
        private void aIToolsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (aiToolWindow == null)
            {
                aiToolWindow = new AIToolWindow();
                aiToolWindow.Setup(this, gameBoard, view, rnd);
                aiToolWindow.Owner = this;

            }

            if (aiToolWindow.Visible)
                aiToolWindow.Hide();
            else
                aiToolWindow.Show();


            
        }



        private void viewMenu_DropDownOpening(object sender, EventArgs e)
        {
            miAITools.Checked = aiToolWindow != null && aiToolWindow.Visible;
        }

        private void gameMenu_DropDownOpening(object sender, EventArgs e)
        {

        }

        private void miBeginner_Click(object sender, EventArgs e)
        {
            newGame(9, 9, 10);
        }

        private void miIntermediate_Click(object sender, EventArgs e)
        {
            newGame(16, 16, 40);
        }

        private void miExpert_Click(object sender, EventArgs e)
        {
            newGame(30, 16, 99);
        }

        private void smileButton_Click_1(object sender, EventArgs e)
        {

        }

        private void miNew_Click(object sender, EventArgs e)
        {
            newGame();
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Close();
        }



 

  


    }
}
